package com.charging.common.utils;

import org.apache.commons.lang3.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalUtils {
    public static BigDecimal getRateDecimal(Long dividend, Long divisor) {
        if (dividend == null || ObjectUtils.firstNonNull(divisor) == 0) {
            return BigDecimal.ZERO;
        }
        return getRateDecimal(BigDecimal.valueOf(dividend), BigDecimal.valueOf(divisor));
    }

    public static BigDecimal getRateDecimal(BigDecimal dividend, BigDecimal divisor) {
        if (dividend == null || ObjectUtils.firstNonNull(divisor).compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        return dividend.multiply(BigDecimal.valueOf(100)).divide(divisor, 2, RoundingMode.HALF_UP);
    }

}
